/*****************************************************************************//*!
* file  : systask.c
* creat : apleilx
* data  : 2017/11/05
*******************************************************************************/

#include "osObjects.h"
#include "pincfg.h"
#define _SYSTASK_MODULE_
#include "systask.h"
#undef _SYSTASK_MODULE_

uint32_t SysTaskCnt;
uint32_t PowerTimeCnt;
uint8_t SysTaskAutoCnt;

void SysTimePulseCreat(void);

/*****************************************************************************//*!
*
* @brief   storage task.
*     
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void SysTask(void const *argument)
{ 
    for(;;){
        osDelay(50);
        SysTimePulseCreat();
        SysTaskCnt++;
        
        if(bSysPulse_500ms){
            LED_CPU = !LED_CPU;
        }
    }
}

/*****************************************************************************//*!
*
* @brief   system time pulse creat.
*     
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
extern uint32_t uwTick;
void SysTimePulseCreat(void){
    static uint32_t uwTickLast;
    volatile uint32_t tick;
    
    uint16_t TickInc;
    
    tick = uwTick;
    TickInc = tick  - uwTickLast;
    uwTickLast = uwTick;

    bSysPulse_1s = 0;
    bSysPulse_500ms = 0;
    bSysPulse_250ms = 0;
    bSysPulse_100ms = 0;
    
    if(!TickInc)
        return;
    
    SysPulseTimeCnt_1s+=TickInc;
    SysPulseTimeCnt_500ms+=TickInc;
    SysPulseTimeCnt_250ms+=TickInc;
    SysPulseTimeCnt_100ms+=TickInc;
    
    if(SysPulseTimeCnt_1s >= (1000/1)){
        SysPulseTimeCnt_1s-= (1000/1);
        bSysPulse_1s = 1;
    }
    
    if(SysPulseTimeCnt_500ms >= (500/1)){
        SysPulseTimeCnt_500ms-= (500/1);
        bSysPulse_500ms = 1;
    }
    
    if(SysPulseTimeCnt_250ms >= (250/1)){
        SysPulseTimeCnt_250ms-= (250/1);
        bSysPulse_250ms = 1;
    }
    
    if(SysPulseTimeCnt_100ms >= (100/1)){
        SysPulseTimeCnt_100ms-= (100/1);
        bSysPulse_100ms = 1;
        
        SysTaskAutoCnt++;
        if(PowerTimeCnt < INT32_MAX)
            PowerTimeCnt++;
    }
}

